﻿function scyzm(obj) {//生成验证码
    var parent = document.getElementById(obj);
    parent.setAttribute("onclick", "location.reload()");
    var can = document.createElement("canvas");
    can.setAttribute("id", "can");
    var context = can.getContext("2d");
    var h = parent.offsetHeight;
    var w = parent.offsetWidth;
    can.width = w;
    can.height = h;
    var i;
    var j;
    var d = "";//答案
    //渐变背景
    var grad = context.createLinearGradient(0, h / 2, w, w / 2);
    var rgbs = new Array();
    for (i = 0; i < 6; i++) {
        rgbs.push(Math.ceil(128 + 127 * Math.random()));
    }
    grad.addColorStop(1, "rgb(" + rgbs[0] + "," + rgbs[1] + "," + rgbs[2] + ")");
    grad.addColorStop(0, "rgb(" + rgbs[3] + "," + rgbs[4] + "," + rgbs[5] + ")");
    context.fillStyle = grad;
    context.fillRect(0, 0, w, h);
    //文字
    var tc;//颜色
    var C;//大写字母
    var c;//小写字母
    var n;//数字
    var k;//文字类型（大、小、数）
    var s;//大小
    var x;//x坐标
    var y;//y坐标
    var r;//旋转角度
    for (i = 0; i < 4; i++) {
        tc = new Array();
        for (j = 0; j < 3; j++) {
            tc.push(Math.ceil(127 * Math.random()));
        }
        context.fillStyle = "rgb(" + tc[0] + "," + tc[1] + "," + tc[2] + ")";//填充颜色
        C = String.fromCharCode(Math.ceil(64 + 26 * Math.random()));
        c = String.fromCharCode(Math.ceil(96 + 26 * Math.random()));
        n = String.fromCharCode(Math.ceil(47 + 10 * Math.random()));
        k = Math.ceil(3 * Math.random());
        s = Math.ceil(15 + 5 * Math.random());
        x = 5 + (i * w / 4);
        y = Math.ceil(s + (h - 2 * s) * Math.random());
        r = Math.ceil(-15 + 60 * Math.random());
        context.font = s + "px 黑体";
        context.translate(x, y); // 将画布的原点移动到正中央
        switch (k) {
            case 1:
                context.rotate((Math.PI / 180) * r);
                context.fillText(C, 0, 0);
                d += C;
                context.rotate((Math.PI / 180) * -r);
                break;
            case 2:
                context.rotate((Math.PI / 180) * r);
                context.fillText(c, 0, 0);
                d += c;
                context.rotate((Math.PI / 180) * -r);
                break;
            case 3:
                context.rotate((Math.PI / 180) * r);
                context.fillText(n, 0, 0);
                d += n;
                context.rotate((Math.PI / 180) * -r);
                break;
        }
        context.translate(-x, -y); // 将画布的原点移动到正中央

    }
    //干扰线
    var spx;//起点x坐标
    var spy;//起点y坐标
    var epx;//终点x坐标
    var epy;//终点y坐标
    for (i = 0; i < 3; i++) {
        tc = new Array();
        for (j = 0; j < 4; j++) {
            tc.push(Math.ceil(127 * Math.random()));
        }
        spx = Math.ceil(w * Math.random());
        epx = Math.ceil(w * Math.random());
        spy = Math.ceil(h * Math.random());
        epy = Math.ceil(h * Math.random());
        context.beginPath();
        context.strokeStyle = "rgb(" + tc[0] + "," + tc[1] + "," + tc[2] + ")";//描边颜色
        context.moveTo(spx, spy);
        context.lineTo(epx, epy);
        context.lineWidth = 1;
        context.stroke();
        context.closePath();//关闭路径
    }

    parent.appendChild(can);
    return d;
}

